package at.fhhagenberg.hydrac.java.sample.ast.codegen.builder;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ArrayCreation;
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.Type;

public class AstBuilder {

	private final AST ast;

	public AstBuilder() {
		ast = AST.newAST(AST.JLS3);
	}

	public ClassBuilder createClass(String packageName, String className) {
		return new ClassBuilder(this, packageName, className);
	}

	public Name newName(String name) {
		return ast.newName(name.split("\\."));
	}

	public AST getAst() {
		return ast;
	}

	public ArrayCreation getNumberArrayInitializer(Type type, String... inits) {
		ArrayCreation arrayCreation = ast.newArrayCreation();
		arrayCreation.setType(ast.newArrayType(type));

		ArrayInitializer initializer = ast.newArrayInitializer();

		for (String init : inits) {
			initializer.expressions().add(ast.newNumberLiteral(init));
		}
		arrayCreation.setInitializer(initializer);

		return arrayCreation;
	}

	public PrimitiveType intType() {
		return ast.newPrimitiveType(PrimitiveType.INT);
	}

	public StringLiteral stringLiteral(String value) {
		StringLiteral literal = ast.newStringLiteral();
		literal.setLiteralValue(value);
		return literal;
	}

}
